﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.General.Entity;
using ESF.SSO.Core.BusinessLogic;

#endregion


namespace ESF.SSO.Presenter.Admin.Menu
{
	public class EditGeneralMenuPresenter : BasePresenter<IEditGeneralMenuViewer>
	{
		#region Constructor

		public EditGeneralMenuPresenter (IEditGeneralMenuViewer pViewer) : base (pViewer) {}

		#endregion


		#region Funciones

		public override void Clear()
		{
			_Viewer.ApplicationID = null;
			_Viewer.lstMainUserMenus = null;			
			_Viewer.SelectedUserMenu = null;
		}


		public override void Load()
		{
			Clear ();
		}


		public UserMenuItem GetUserMenu (UserMenuItem pUserMenuItem)
		{
			try
			{
				return CtrlUser.GetUserMenu (pUserMenuItem);
			}
			catch (Exception pEx)
			{
				LogError (pEx);

				return null;
			}
		}


		public int? SaveUserMenu (UserMenuItem pUserMenuItem)
		{
			int? menuID;


			try
			{
				pUserMenuItem.UserCreationAlias = _Viewer.frmUserAlias;
				pUserMenuItem.UserLastUpdateAlias = _Viewer.frmUserAlias;
				pUserMenuItem.CreationDateTime = DateTime.Now;
				pUserMenuItem.LastUpdateDateTime = DateTime.Now;

				menuID = CtrlUser.RegisterUserMenu (pUserMenuItem);

				_Viewer.frmInfoMsg = "El Elemento de Menu ha sido almacenado";

				return menuID;
			}
			catch (Exception pEx)
			{
				LogError (pEx);

				return null;
			}
		}


		public void DeleteUserMenu (UserMenuItem pUserMenuItem)
		{
			try
			{
				CtrlUser.DeleteUserMenu (pUserMenuItem);

				_Viewer.frmInfoMsg = "El Elemento de Menu ha sido eliminado";
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public IEnumerable<UserMenuItem> GetUserMenusByLevel(int? pExpandedUserMenuID)
		{
			try
			{
				return CtrlUser.GetUserMenus (
					new General.Entity.BasicReference () { ID = _Viewer.ApplicationID },
					new General.Entity.BasicReference () { ID = pExpandedUserMenuID });
			}
			catch (Exception pEx)
			{
				LogError (pEx);

				return null;
			}
		} 


		public void ShowMainUserMenus ()
		{
			if (_Viewer.ApplicationID == null)
			{
				_Viewer.lstMainUserMenus = null;
				return;
			}

			try
			{
				_Viewer.lstMainUserMenus = CtrlUser.GetMainUserMenus (
					new General.Entity.BasicReference () { ID = _Viewer.ApplicationID }).ToList ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		} 

		#endregion
	}
}
